/* 提示信息 */
// const loginFail = {
//   isPrompt: true,
//   isCloseEl: true,
//   isConfirm: true,
//   title: "登录失效",
//   info: "请返回集分竞宝APP重新进入此页",
//   isMaskDown: false,
//   textAlign: "center"
// };
const AjaxMixin = {
  data() {
    return {
      currentPageNo: 0,
      // 头部导航高度
      headerElHeight: 0,
      // 屏幕高度
      windowHeight: 0,
      // 主体高度
      mainHeight: 0
    };
  },
  mounted() {
    const windowHeight = this.getHeight();
    this.windowHeight = windowHeight;
    const headerElHeight = this.getElHeight(document.querySelector("#header"));
    this.headerElHeight = headerElHeight;
    // 计算主体高度
    this.mainHeight = windowHeight - headerElHeight;
    this.resetHeight();
  },

  methods: {
    /**
     * 重置高度
     */
    resetHeight() {
      const windowHeight = this.getHeight();
      document.body.style.height = `${windowHeight}px`;
    },
    setStyle() {},
    // 获取窗口暂时放这里
    getHeight() {
      if (window.innerHeight !== undefined) {
        return window.innerHeight;
      }
      const B = document.body;
      const D = document.documentElement;
      return Math.min(D.clientHeight, B.clientHeight);
    },
    /**
     * 获取单个元素高度
     * 包含padding，border
     * @param [Element] el html元素
     */
    getElHeight(el) {
      const height = parseFloat(this.getStyle(el, "height"), 10);
      const paddingTop = parseFloat(this.getStyle(el, "paddingTop"), 10);
      const paddingBottom = parseFloat(this.getStyle(el, "paddingBottom"), 10);
      const borderTop = parseFloat(this.getStyle(el, "borderTopWidth"), 10);
      const borderBottom = parseFloat(this.getStyle(el, "borderBottomWidth"), 10);
      return height + paddingTop + paddingBottom + borderTop + borderBottom;
    },
    // 获取窗口暂时放这里
    getWidth() {
      if (window.innerWidth !== undefined) {
        return window.innerWidth;
      }
      const B = document.body;
      const D = document.documentElement;
      return Math.min(D.clientWidth, B.clientWidth);
    },
    // 获取当前样式
    getStyle(element, att) {
      if (window.getComputedStyle) {
        // 优先使用W3C规范
        return window.getComputedStyle(element)[att];
      }
      // 针对IE9以下兼容
      return element.currentStyle[att];
    },
    /**
     * Ajax Post请求
     * @param {String} url 请求地址
     * @param {Object} options 请求参数
     */
    post(url, options) {
      // const touchHome = "http://touch.ihaveu.com";
      /* eslint-disable */
      const token = this.getToken();
      return this.$http({
        method: "POST",
        url: url,
        params: options,
        headers: {
          accessToken: token
        }
      }).then(res => {
        const status = res.body.execErrCode;
        if (status === "4003") {
          this.$router.replace(`/login?fullPath=${encodeURIComponent(this.$route.fullPath)}`);
          localStorage.removeItem("token");
          // 登录失效弹框
          // this.$emit("onPrompt", loginFail);
          return res.body;
        }
        if (status !== "200" && status !== "1140") {
          const errorObj = {
            isPrompt: true,
            isCloseEl: true,
            isConfirm: false,
            title: "提示",
            info: res.body.execMsg,
            isMaskDown: true,
            textAlign: "center"
          };
          this.$emit("onPrompt", errorObj);
        }
        return res.body;
        // throw new Error(`请求错误，错误信息：${res.body.execMsg};错误代码为：${res.body.execErrCode}`);
      });
    },
    /**
     * Ajax Get请求
     * @param {String} url 请求地址
     * @param {Object} options 请求参数
     */
    get(url, options) {
      /* eslint-disable */
      const token = this.getToken();
      return this.$http({
        method: "GET",
        url: url,
        params: options,
        headers: {
          accessToken: token
        }
      }).then(res => {
        let flag = false;
        switch (res.body.execErrCode) {
          case "200":
            flag = true;
            break;
          case "1110": // 商品已被拍下等待付款，无法取消
            flag = true;
            break;
          default:
            break;
        }
        if (flag) {
          return res.body;
        }
        throw new Error(
          `请求错误，错误信息：${res.body.execMsg};错误代码为：${
            res.body.execErrCode
          }`
        );
      });
    },
    point(value) {
      let point = +value;
      point = point.toFixed(3);
      return +point.toString().substr(0, point.toString().length - 1);
    },
    /**
     * 获取token
     */
    getToken() {
      let _token;
      const re = /\?newToken=(.*)/;
      const { search } = window.location;
      const tokenSearch = re.exec(search);
      if (tokenSearch) {
        _token = tokenSearch[1];
      } else {
        _token = localStorage.getItem("token") || false;
      }
      return _token;
    },
    /**
     * 设置cookie
     * @param {String} name cookie名称
     * @param {String} value cookie值
     * @param {String|Number} expiresDays 设置cookie的存活周期
     */
    setCookie(name, value, expiresDays) {
      expiresDays = expiresDays || 30;
      let exp = new Date();
      exp.setTime(exp.getTime() + expiresDays * 24 * 60 * 60 * 1000);
      document.cookie =
        name + "=" + escape(value) + ";expires=" + exp.toGMTString();
    },
    /**
     * 获取cookie
     * @param {String} name cookie名称
     */
    getCookie(name) {
      let arr,
        reg = new RegExp("(^| )" + name + "=([^;]*)(;|$)");
      if ((arr = document.cookie.match(reg))) return unescape(arr[2]);
      else return false;
    }
  }
};

export default AjaxMixin;
